<?php
namespace app\api\controller;
use app\api\common\controller\Api;
use think\facade\Db;
use app\common\controller\Note;

/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Common extends Api
{

	//会员支付(余额满足要求，请求输入验证码)
	public function membershipPayment(){
		$mobile = $this->request::param('mobile');
		$code = $this->request::param('code');
		$store_id = $this->request::param('store_id');
		$note = Db::name('note') -> where(['mobile' => $mobile,'store_id' => $store_id,'code' => $code]) -> find();
		if(!$note){
			$this->result("验证码错误", [], 400,'json');
		}
		if(time() > $note['out_date_time']){
			$this->result("验证码过期，请重新获取", [], 400,'json');
		}
		$this->result("OK", [], 200,'json');

	}
	//获取验证码
	public function getCode(){
		$mobile = $this->request::param('mobile');
		$store_id = $this->request::param('store_id');
		$interflow = interflow($store_id);
		$ids = Db::name('membership_records') -> whereIn('store_id',$interflow) -> where('mobile',$mobile) -> column('id');
		if(count($ids) == 0){
			$this->result("该手机号不是本店会员", [], 400,'json');
		}
	    $note = Db::name('note') -> where(['mobile' => $mobile,'store_id' => $store_id]) -> where('out_date_time','>=',time()) -> find();
	    if($note){
	    	$this->result("验证码已发送成功", [], 400,'json');
	    }
	    $amount = Db::name('summary') -> whereIn('card_id',$ids) -> sum('left_amount');
		(new Note($mobile,$store_id))->setCode();
		$this->result("发送成功", $amount, 200,'json');
		
	}


}